package org.msfj.authority.server.util;

import org.msfj.authority.client.config.JwtConfig;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import org.msfj.common.exception.TokenErrorException;
import org.msfj.common.util.jwt.IJWTInfo;
import org.msfj.common.util.jwt.JWTHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MemberJwtTokenUtil {

    @Autowired
    JwtConfig jwtConfig;

    public String generateToken(IJWTInfo jwtInfo) throws Exception {
        return JWTHelper.generateToken(jwtInfo,jwtConfig.getPriKeyPath(),jwtConfig.getMemberExpire().intValue());
    }

    public IJWTInfo getInfoFromToken(String token) throws Exception {
        try {
            return JWTHelper.getInfoFromToken(token,jwtConfig.getPubKeyPath());
        }catch (ExpiredJwtException ex){
            throw new TokenErrorException("用户token过期");
        }catch (SignatureException ex){
            throw new TokenErrorException("用户token签名错误");
        }catch (IllegalArgumentException ex){
            throw new TokenErrorException("用户token为空");
        }
    }
}
